package com.caffedev.ottollotto;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.ContentValues;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;




public class OttollottoMyNum extends ListActivity {
	
	//private String GRWNO;
	//private String GRWDate;
/*	private String FirstBall;
	private String SecondBall;
	private String ThirdBall;
	private String FourthBall;
	private String FifthBall;
	private String SixthBall;*/
	private final static String TAG="OttollottoMyNum";
	private String AddDate;
	private String Result;
	
	
	private static final int ID=0;
	private static final int INDEX_GRWNO=1;
	private static final int INDEX_GRWDATE=2;
	private static final int INDEX_FIRSTBALL=3;
	private static final int INDEX_SECONDBALL=4;
	private static final int INDEX_THIRDBALL=5;
	private static final int INDEX_FOURTHBALL=6;
	private static final int INDEX_FIFTHBALL=7;
	private static final int INDEX_SIXTHBALL=8;
	private static final int INDEX_ADD_DATE=9;
	private static final int INDEX_RESULT=10;
	private static final int INDEX_MAGIC=11;
	
	private static final String KEY_ID="_id";
	private static final String KEY_GRWNO="grwno";
	private static final String KEY_GRWDATE="grwdate";
	private static final String KEY_FIRSTBALL="firstball";
	private static final String KEY_SECONDBALL="secondball";
	private static final String KEY_THIRDBALL="thirdball";
	private static final String KEY_FOURTHBALL="fourthball";
	private static final String KEY_FIFTHBALL="fifthball";
	private static final String KEY_SIXTHBALL="sixthball";
	private static final String KEY_ADD_DATE="adddate";
	private static final String KEY_RESULT="result";
	private static final String KEY_MAGIC = "magic";
	
	private Button add_new_number;

	
	//private DatabaseHelper mHelper;
	//private SQLiteDatabase mDb;
	public static Cursor sCursor;
	
	private static final String TABLE_NAME = "sTable";
	
	private myCursorAdapter ca;
	
	
	
	private AlertDialog.Builder builder;
    private AlertDialog dialog;
    
    
    
    private int ballList_index;
	private TextView list;
    
	
	private static final int REQ_ADD_NUM = 1;
	private static final int REQ_LUCK_NUM = 2;
	
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		setContentView(R.layout.mynum);
		
		add_new_number = (Button)findViewById(R.id.add_new);

		
		add_new_number.setOnClickListener(listener);

	
		
		//mHelper = new DatabaseHelper(this);
	    //mDb = mHelper.getWritableDatabase();
		    
		    String query = "select * from "+TABLE_NAME+" order by "+KEY_GRWNO+" desc";
		    sCursor = OttollottoMain.mDb.rawQuery(query, null);
	        sCursor.moveToFirst();
	        startManagingCursor(sCursor);
	        
	        ca = new myCursorAdapter(this, sCursor);

	        setListAdapter(ca);
	}

	private View.OnClickListener listener = new View.OnClickListener() {
		
		public void onClick(View v) {
			switch(v.getId()){
			case R.id.add_new:
				Intent intent = new Intent(OttollottoMyNum.this, OttollottoAddNum.class);
				startActivityForResult(intent, REQ_ADD_NUM);
				break;

			}

			
		}
	};
	
	
	protected void onActivityResult (int requestCode, int resultCode, Intent intent){
		switch(requestCode){
		case REQ_ADD_NUM:
			if(resultCode == RESULT_OK){
				sCursor.requery();
				ca.notifyDataSetChanged();
			}
			break;
		case REQ_LUCK_NUM:
			if(resultCode == RESULT_OK){
				String no = intent.getStringExtra("grwno");
				int grade = intent.getIntExtra("grade", 0);
				
				ContentValues arg = new ContentValues();
				arg.put(KEY_RESULT, grade);
				OttollottoMain.mDb.update(TABLE_NAME, arg, KEY_ID+"="+CurrentID, null);
				ca.notifyDataSetChanged();
			}
			break;
			
		}
	}
	
private int CurrentID;
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		//Log.e(TAG,"position : "+ position);
		new AlertDialog.Builder(OttollottoMyNum.this)
		.setTitle(R.string.app_name)
		.setIcon(R.drawable.icon)
		.setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				/*if(Integer.valueOf(OttollottoMain.GRWNO) < Integer.valueOf(sCursor.getString(INDEX_GRWNO))){
					Toast.makeText(OttollottoMyNum.this,R.string.not_yet, Toast.LENGTH_SHORT).show();
					return;
				}
				*/
				CurrentID = sCursor.getInt(ID);
				String[] nums={sCursor.getString(INDEX_FIRSTBALL),sCursor.getString(INDEX_SECONDBALL),sCursor.getString(INDEX_THIRDBALL),
						sCursor.getString(INDEX_FOURTHBALL),sCursor.getString(INDEX_FIFTHBALL),sCursor.getString(INDEX_SIXTHBALL)};
				
				Intent intent = new Intent(OttollottoMyNum.this, OttollottoWin.class);
				
				intent.putExtra("number", nums);
				intent.putExtra("grwno", sCursor.getString(INDEX_GRWNO));
				
				startActivityForResult(intent, REQ_LUCK_NUM);
				
				
			}
		})
		.setNegativeButton(R.string.delete, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				
				OttollottoMain.mDb.delete(TABLE_NAME, KEY_ID+"="+sCursor.getString(ID), null);
				sCursor.requery();
				ca.notifyDataSetChanged();
			}
		})
		.show();
		
	}

	
	

	

	
	




	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}
	
	



	/////////////////////// custom CursorAdapter
	public class myCursorAdapter extends CursorAdapter{
		
		
		public myCursorAdapter(Context ctx, Cursor cursor){
			super(ctx, cursor,true);
		}
		
		public void bindView(View view, Context context, Cursor cursor) {
		
			TextView grwno = (TextView)view.findViewById(R.id.grwno);
			TextView magic = (TextView)view.findViewById(R.id.magic);
			ImageView ball1 = (ImageView)view.findViewById(R.id.image_ball1);
			ImageView ball2 = (ImageView)view.findViewById(R.id.image_ball2);
			ImageView ball3 = (ImageView)view.findViewById(R.id.image_ball3);
			ImageView ball4 = (ImageView)view.findViewById(R.id.image_ball4);
			ImageView ball5 = (ImageView)view.findViewById(R.id.image_ball5);
			ImageView ball6 = (ImageView)view.findViewById(R.id.image_ball6);
			ImageView result = (ImageView)view.findViewById(R.id.image_result);
			
			grwno.setText(cursor.getString(INDEX_GRWNO));//+getString(R.string.round));
			magic.setText(cursor.getString(INDEX_MAGIC));
			ball1.setBackgroundResource(getResources().getIdentifier("b" +cursor.getString(INDEX_FIRSTBALL), "drawable", getPackageName()));
			ball2.setBackgroundResource(getResources().getIdentifier("b" +cursor.getString(INDEX_SECONDBALL), "drawable", getPackageName()));
			ball3.setBackgroundResource(getResources().getIdentifier("b" +cursor.getString(INDEX_THIRDBALL), "drawable", getPackageName()));
			ball4.setBackgroundResource(getResources().getIdentifier("b" +cursor.getString(INDEX_FOURTHBALL), "drawable", getPackageName()));
			ball5.setBackgroundResource(getResources().getIdentifier("b" +cursor.getString(INDEX_FIFTHBALL), "drawable", getPackageName()));
			ball6.setBackgroundResource(getResources().getIdentifier("b" +cursor.getString(INDEX_SIXTHBALL), "drawable", getPackageName()));
			
			
			result.setBackgroundResource(getResources().getIdentifier("r" +cursor.getString(INDEX_RESULT), "drawable", getPackageName()));
		
		}
		
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
		//LayoutInflater inflater = ((Activity)this).getLayoutInflater();
		LayoutInflater inflater = LayoutInflater.from(context);
		View row = (View)inflater.inflate(R.layout.srow, parent,false);
		
		
		return row;
		}
	}
	
}
